WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
problem.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Fwd refs
22namespace wolf
23{
24class SolverManager;
25class HardwareBase;
26class TrajectoryBase;
27class MapBase;
28class ProcessorMotion;
29class StateBlock;
30class TimeStamp;
31struct ParamsSensorBase;
32struct ParamsProcessorBase;
33} // namespace wolf
34
35// wolf includes
36#include "core/common/wolf.h"
37#include "core/utils/loader.h"
45
46// std includes
47#include <mutex>
48
49// yaml includes
50#include "yaml-cpp/yaml.h"
51
52namespace wolf
53{
55{
57 REMOVE
58};
59
60// Class Problem
61class Problem : public std::enable_shared_from_this<Problem>
62{
63 friend SolverManager; // Enable SolverManager to acces protected functions (consumeXXXNotificationMap())
64 friend ProcessorBase;
65 friend ProcessorMotion;
66 friend MotionProvider;
67
68 protected:
69 HardwareBasePtr hardware_ptr_;
70 TrajectoryBasePtr trajectory_ptr_;
71 MapBasePtr map_ptr_;
72 TreeManagerBasePtr tree_manager_;
73 std::map<unsigned int, MotionProviderPtr> motion_provider_map_;
74
79 unsigned int first_frame_status_;
80
81 std::map<std::pair<StateBlockConstPtr, StateBlockConstPtr>, Eigen::MatrixXd> covariances_;
82
83 std::map<FactorBasePtr, Notification> factor_notification_map_;
84 std::map<StateBlockPtr, Notification> state_block_notification_map_;
85 mutable std::mutex mut_notifications_;
86 mutable std::mutex mut_covariances_;
87
88 std::atomic_bool transformed_;
90 mutable std::mutex mut_transform_;
91
93
94 private: // CAUTION: THESE METHODS ARE PRIVATE, DO NOT MAKE THEM PUBLIC !!
95 Problem(SizeEigen _dim,
96 const TypeComposite& _frame_types = {},
97 LoaderPtr _loader = nullptr); // USE create() below !!
98 void setup();
99
100 public:
101 static ProblemPtr create(SizeEigen _dim,
102 const TypeComposite& _frame_types = {},
103 LoaderPtr _loader = nullptr); // USE THIS AS A CONSTRUCTOR!
104 static ProblemPtr autoSetup(const std::string& _input_yaml_file,
105 std::vector<std::string> _primary_schema_folders = {},
106 LoaderPtr _loader = nullptr,
107 bool _skip_yaml_validation = false);
108 static ProblemPtr autoSetup(YAML::Node _param_node,
109 std::vector<std::string> _primary_schema_folders = {},
110 LoaderPtr _loader = nullptr,
111 bool _skip_yaml_validation = false);
112 virtual ~Problem();
113
114 // Properties -----------------------------------------
116 SizeEigen getDim() const;
118
119 protected:
120 void addFrameTypes(const TypeComposite& _types);
121
122 // Tree manager --------------------------------------
123 public:
124 void setTreeManager(TreeManagerBasePtr _gm);
125 TreeManagerBaseConstPtr getTreeManager() const;
126 TreeManagerBasePtr getTreeManager();
127
128 // Hardware branch ------------------------------------
129 HardwareBaseConstPtr getHardware() const;
130 HardwareBasePtr getHardware();
131
137 SensorBasePtr installSensor(YAML::Node _sensor_node, std::vector<std::string> _schema_folders = {});
138
144 SensorBasePtr installSensor(const std::string& _params_yaml_filename, std::vector<std::string> _schema_folders);
145
151 ProcessorBasePtr installProcessor(YAML::Node _processor_node, std::vector<std::string> _schema_folders = {});
152
159 ProcessorBasePtr installProcessor(SensorBasePtr _sensor_corresponding,
160 const std::string& _params_yaml_filename,
161 std::vector<std::string> _schema_folders);
162
171 ProcessorBasePtr installProcessor(const std::string& _params_yaml_filename,
172 std::vector<std::string> _schema_folders);
173
177 SensorBaseConstPtr findSensor(const std::string& _sensor_name) const;
178 SensorBasePtr findSensor(const std::string& _sensor_name);
179
183 ProcessorBaseConstPtr findProcessor(const std::string& _processor_name) const;
184 ProcessorBasePtr findProcessor(const std::string& _processor_name);
185
186 protected:
191 void addMotionProvider(MotionProviderPtr _processor_motion_ptr);
192 void removeMotionProvider(MotionProviderPtr proc);
193
194 public:
195 std::map<unsigned int, MotionProviderConstPtr> getMotionProviderMap() const;
196 std::map<unsigned int, MotionProviderPtr> getMotionProviderMap();
197
198 // Trajectory branch ----------------------------------
199 TrajectoryBaseConstPtr getTrajectory() const;
200 TrajectoryBasePtr getTrajectory();
201
202 // First Frame
203 bool isFirstFrameSpecified() const;
204 bool isFirstFrameApplied() const;
205 void setFirstFrameOptions(const YAML::Node& _first_frame_node);
206 void setFirstFrameOptions(const TypeComposite& _first_frame_types,
207 const VectorComposite& _first_frame_values,
208 const PriorComposite& _first_frame_priors);
209 FrameBasePtr applyFirstFrameOptions(const TimeStamp& _ts);
210 FrameBasePtr emplaceFirstFrame(const TimeStamp& _ts, const YAML::Node& _first_frame_node);
211 FrameBasePtr emplaceFirstFrame(const TimeStamp& _ts,
212 const TypeComposite& _first_frame_types,
213 const VectorComposite& _first_frame_values,
214 const PriorComposite& _first_frame_priors);
215
227 FrameBasePtr emplaceFrame(const TimeStamp& _time_stamp,
228 const TypeComposite& _frame_types,
229 const VectorComposite& _frame_state,
230 const PriorComposite& _frame_priors = {});
231
244 FrameBasePtr emplaceFrame(const TimeStamp& _time_stamp, const StateKeys& _frame_keys);
245
257 FrameBasePtr emplaceFrame(const TimeStamp& _time_stamp, const VectorComposite& _frame_state);
258
271 FrameBasePtr emplaceFrame(const TimeStamp& _time_stamp,
272 const StateKeys& _frame_keys,
273 const Eigen::VectorXd& _frame_state);
274
284 void emplaceStatesToFrame(FrameBasePtr _frame_ptr, const StateKeys& _frame_keys);
285
294 void emplaceStatesToFrame(FrameBasePtr _frame_ptr, const VectorComposite& _frame_states);
295
296 // Frame getters
297 FrameBaseConstPtr getLastFrame() const;
298 FrameBasePtr getLastFrame();
299 FrameBaseConstPtr closestFrameToTimeStamp(const TimeStamp& _ts) const;
300 FrameBasePtr closestFrameToTimeStamp(const TimeStamp& _ts);
301
309 bool permitKeyFrame(ProcessorBaseConstPtr _processor_ptr) const;
310
316 void keyFrameCallback(FrameBasePtr _keyframe_ptr, ProcessorBasePtr _processor_ptr);
317
318 // State getters
319 TimeStamp getTimeStamp() const;
320 VectorComposite getState(const StateKeys& _keys) const;
321 VectorComposite getState(const TimeStamp& _ts, const StateKeys& _keys) const;
322 VectorComposite getOdometry(const StateKeys& _keys) const;
323
324 // Zero state provider
325 VectorComposite stateZero(TypeComposite _types) const;
326 VectorComposite stateZero(const StateKeys& _keys) const;
327
328 // Map branch -----------------------------------------
329 MapBaseConstPtr getMap() const;
330 MapBasePtr getMap();
331 void setMap(MapBasePtr);
332 bool saveMap(const std::string& _filename_dot_yaml,
333 const std::string& _map_name = "Map automatically saved by Wolf");
334
335 // All branches -------------------------------------------
340 void perturb(double amplitude = 0.01);
341 // transform states
342 void transform(const VectorComposite& _transformation);
343 bool isTransformed() const;
344 void resetTransformed();
345 VectorComposite getTransformation() const;
346
347 // Covariances
348 void clearCovariance();
349 void addCovarianceBlock(StateBlockPtr _state1, StateBlockPtr _state2, const Eigen::MatrixXd& _cov);
350 void addCovarianceBlock(StateBlockPtr _state1, const Eigen::MatrixXd& _cov);
351 bool getCovarianceBlock(StateBlockConstPtr _state1,
352 StateBlockConstPtr _state2,
353 Eigen::MatrixXd& _cov,
354 const int _row = 0,
355 const int _col = 0) const;
356 bool getCovarianceBlock(std::map<StateBlockConstPtr, unsigned int> _sb_2_idx, Eigen::MatrixXd& _cov) const;
357 bool getCovarianceBlock(StateBlockConstPtr _state, Eigen::MatrixXd& _cov, const int _row_and_col = 0) const;
358 bool getCovariance(NodeStateBlocksConstPtr _has_states_ptr,
359 const StateKeys& _keys,
360 Eigen::MatrixXd& _covariance) const;
361 bool getLastFrameCovariance(const StateKeys& _keys, Eigen::MatrixXd& _covariance) const;
362
363 // Solver management ----------------------------------------
364
367 StateBlockPtr notifyStateBlock(StateBlockPtr _state_ptr, Notification _notif);
368
372
375 bool getStateBlockNotification(const StateBlockPtr& sb_ptr, Notification& notif) const;
376
379 FactorBasePtr notifyFactor(FactorBasePtr _factor_ptr, Notification _notif);
380
384
387 bool getFactorNotification(const FactorBasePtr& fac_ptr, Notification& notif) const;
388
389 protected:
393 void consumeNotifications(std::map<StateBlockPtr, Notification>&, std::map<FactorBasePtr, Notification>&);
394
398 std::map<StateBlockPtr, Notification> consumeStateBlockNotificationMap();
399
402 std::map<FactorBasePtr, Notification> consumeFactorNotificationMap();
403
404 public:
405 // Print and check ---------------------------------------
413 void print(int depth = 4, //
414 bool factored_by = false, //
415 bool metric = true, //
416 bool state_blocks = false,
417 std::ostream& stream = std::cout) const;
418 bool check(int verbose_level = 0) const;
419 bool check(bool verbose, std::ostream& stream) const;
420};
421
422} // namespace wolf
423
424// IMPLEMENTATION
425
426namespace wolf
427{
429{
430 return loader_;
431}
432
433inline TreeManagerBaseConstPtr Problem::getTreeManager() const
434{
435 return tree_manager_;
436}
437
438inline TreeManagerBasePtr Problem::getTreeManager()
439{
440 return tree_manager_;
441}
442
444{
445 return first_frame_status_ == 1 || first_frame_status_ == 2;
446}
447
449{
450 return first_frame_status_ == 2;
451}
452
453inline std::map<unsigned int, MotionProviderConstPtr> Problem::getMotionProviderMap() const
454{
455 std::map<unsigned int, MotionProviderConstPtr> map_const;
456 for (auto&& pair : motion_provider_map_) map_const[pair.first] = pair.second;
457 return map_const;
458}
459
460inline std::map<unsigned int, MotionProviderPtr> Problem::getMotionProviderMap()
461{
463}
464
466{
467 std::lock_guard<std::mutex> lock(mut_notifications_);
468 return state_block_notification_map_.size();
469}
470
472{
473 std::lock_guard<std::mutex> lock(mut_notifications_);
474 return factor_notification_map_.size();
475}
476
477inline void Problem::consumeNotifications(std::map<StateBlockPtr, Notification>& _sb_notification_map,
478 std::map<FactorBasePtr, Notification>& _fac_notification_map)
479{
480 std::lock_guard<std::mutex> lock(mut_notifications_);
481
482 _sb_notification_map = std::move(state_block_notification_map_);
483 _fac_notification_map = std::move(factor_notification_map_);
484}
485
486} // namespace wolf
Definition: motion_provider.h:33
Definition: problem.h:62
bool getStateBlockNotification(const StateBlockPtr &sb_ptr, Notification &notif) const
Returns if the state block has been notified, and the notification via parameter.
Definition: problem.cpp:917
bool permitKeyFrame(ProcessorBaseConstPtr _processor_ptr) const
Give the permission to a processor to create a new key Frame.
Definition: problem.cpp:852
ProcessorBaseConstPtr findProcessor(const std::string &_processor_name) const
get a processor pointer by its name
Definition: problem.cpp:444
void addCovarianceBlock(StateBlockPtr _state1, StateBlockPtr _state2, const Eigen::MatrixXd &_cov)
Definition: problem.cpp:969
bool saveMap(const std::string &_filename_dot_yaml, const std::string &_map_name="Map automatically saved by Wolf")
Definition: problem.cpp:1267
void setFirstFrameOptions(const YAML::Node &_first_frame_node)
Definition: problem.cpp:1181
VectorComposite first_frame_values_
Definition: problem.h:77
bool isFirstFrameSpecified() const
Definition: problem.h:443
bool isFirstFrameApplied() const
Definition: problem.h:448
TrajectoryBasePtr trajectory_ptr_
Definition: problem.h:70
void setMap(MapBasePtr)
Definition: problem.cpp:1136
SizeStd getFactorNotificationMapSize() const
Returns the size of the map of factor notification.
Definition: problem.h:471
HardwareBaseConstPtr getHardware() const
Definition: problem.cpp:1151
bool isTransformed() const
Definition: problem.cpp:1365
MapBaseConstPtr getMap() const
Definition: problem.cpp:1126
std::mutex mut_transform_
Definition: problem.h:90
void print(int depth=4, bool factored_by=false, bool metric=true, bool state_blocks=false, std::ostream &stream=std::cout) const
print wolf tree
Definition: problem.cpp:1272
void resetTransformed()
Definition: problem.cpp:1369
void keyFrameCallback(FrameBasePtr _keyframe_ptr, ProcessorBasePtr _processor_ptr)
New key frame callback.
Definition: problem.cpp:863
TreeManagerBaseConstPtr getTreeManager() const
Definition: problem.h:433
TypeComposite frame_types_
Definition: problem.h:76
bool getCovarianceBlock(StateBlockConstPtr _state1, StateBlockConstPtr _state2, Eigen::MatrixXd &_cov, const int _row=0, const int _col=0) const
Definition: problem.cpp:987
LoaderPtr loader_
Definition: problem.h:92
FactorBasePtr notifyFactor(FactorBasePtr _factor_ptr, Notification _notif)
Notifies a new/removed factor to update the solver manager.
Definition: problem.cpp:926
bool getLastFrameCovariance(const StateKeys &_keys, Eigen::MatrixXd &_covariance) const
Definition: problem.cpp:1121
std::map< StateBlockPtr, Notification > consumeStateBlockNotificationMap()
Returns the map of state block notification to be handled by the solver (the map stored in this is em...
static ProblemPtr autoSetup(const std::string &_input_yaml_file, std::vector< std::string > _primary_schema_folders={}, LoaderPtr _loader=nullptr, bool _skip_yaml_validation=false)
Definition: problem.cpp:80
VectorComposite transformation_
Definition: problem.h:89
TypeComposite getFrameTypes(StateKeys _keys) const
Definition: problem.cpp:762
std::map< FactorBasePtr, Notification > factor_notification_map_
Definition: problem.h:83
void perturb(double amplitude=0.01)
perturb all states states with random noise following an uniform distribution in [ -amplitude,...
Definition: problem.cpp:1328
void setTreeManager(TreeManagerBasePtr _gm)
Definition: problem.cpp:794
void transform(const VectorComposite &_transformation)
Definition: problem.cpp:1344
FrameBasePtr emplaceFrame(const TimeStamp &_time_stamp, const TypeComposite &_frame_types, const VectorComposite &_frame_state, const PriorComposite &_frame_priors={})
Emplace frame from timestamp, types and and state.
Definition: problem.cpp:460
StateBlockPtr notifyStateBlock(StateBlockPtr _state_ptr, Notification _notif)
Notifies a new/removed state block to update the solver manager.
Definition: problem.cpp:894
FrameBaseConstPtr getLastFrame() const
Definition: problem.cpp:1161
std::map< unsigned int, MotionProviderConstPtr > getMotionProviderMap() const
Definition: problem.h:453
SizeEigen getDim() const
Definition: problem.cpp:757
bool getCovariance(NodeStateBlocksConstPtr _has_states_ptr, const StateKeys &_keys, Eigen::MatrixXd &_covariance) const
Definition: problem.cpp:1082
SizeEigen dim_
Definition: problem.h:75
static ProblemPtr create(SizeEigen _dim, const TypeComposite &_frame_types={}, LoaderPtr _loader=nullptr)
Definition: problem.cpp:71
SizeStd getStateBlockNotificationMapSize() const
Returns the size of the map of state block notification.
Definition: problem.h:465
virtual ~Problem()
Definition: problem.cpp:192
void addFrameTypes(const TypeComposite &_types)
Definition: problem.cpp:773
LoaderPtr getLoader()
Definition: problem.h:428
void addMotionProvider(MotionProviderPtr _processor_motion_ptr)
Set the processor motion.
Definition: problem.cpp:801
std::atomic_bool transformed_
Definition: problem.h:88
VectorComposite stateZero(TypeComposite _types) const
Definition: problem.cpp:833
void clearCovariance()
Definition: problem.cpp:963
std::map< FactorBasePtr, Notification > consumeFactorNotificationMap()
Returns the map of factor notification to be handled by the solver (the map stored in this is emptied...
FrameBasePtr emplaceFirstFrame(const TimeStamp &_ts, const YAML::Node &_first_frame_node)
Definition: problem.cpp:1252
VectorComposite getState(const StateKeys &_keys) const
Definition: problem.cpp:587
std::mutex mut_covariances_
Definition: problem.h:86
void consumeNotifications(std::map< StateBlockPtr, Notification > &, std::map< FactorBasePtr, Notification > &)
Returns the map of state block notification to be handled by the solver (the map stored in this is em...
Definition: problem.h:477
bool check(int verbose_level=0) const
Definition: problem.cpp:1323
VectorComposite getOdometry(const StateKeys &_keys) const
Definition: problem.cpp:711
std::map< StateBlockPtr, Notification > state_block_notification_map_
Definition: problem.h:84
FrameBaseConstPtr closestFrameToTimeStamp(const TimeStamp &_ts) const
Definition: problem.cpp:1171
VectorComposite getTransformation() const
Definition: problem.cpp:1373
TimeStamp getTimeStamp() const
Definition: problem.cpp:567
bool getFactorNotification(const FactorBasePtr &fac_ptr, Notification &notif) const
Returns if the factor has been notified, and the notification via parameter.
Definition: problem.cpp:954
FrameBasePtr applyFirstFrameOptions(const TimeStamp &_ts)
Definition: problem.cpp:1225
MapBasePtr map_ptr_
Definition: problem.h:71
ProcessorBasePtr installProcessor(YAML::Node _processor_node, std::vector< std::string > _schema_folders={})
Factory method to install (create, and add to sensor) processors only from a YAML node – Helper metho...
Definition: problem.cpp:288
std::map< std::pair< StateBlockConstPtr, StateBlockConstPtr >, Eigen::MatrixXd > covariances_
Definition: problem.h:81
unsigned int first_frame_status_
0: not set, 1: set, 2: applied
Definition: problem.h:79
HardwareBasePtr hardware_ptr_
Definition: problem.h:69
TreeManagerBasePtr tree_manager_
Definition: problem.h:72
std::map< unsigned int, MotionProviderPtr > motion_provider_map_
Definition: problem.h:73
SensorBasePtr installSensor(YAML::Node _sensor_node, std::vector< std::string > _schema_folders={})
Factory method to install (create and add) sensors only from a YAML node – Helper method loading para...
Definition: problem.cpp:197
SensorBaseConstPtr findSensor(const std::string &_sensor_name) const
get a sensor pointer by its name
Definition: problem.cpp:434
std::mutex mut_notifications_
Definition: problem.h:85
void emplaceStatesToFrame(FrameBasePtr _frame_ptr, const StateKeys &_frame_keys)
Take a frame and emplace to it the missing states with the frame timestamp.
Definition: problem.cpp:526
void removeMotionProvider(MotionProviderPtr proc)
Definition: problem.cpp:826
PriorComposite first_frame_priors_
Definition: problem.h:78
TrajectoryBaseConstPtr getTrajectory() const
Definition: problem.cpp:1141
Definition: processor_base.h:118
class for Motion processors
Definition: processor_motion.h:110
Solver manager for WOLF.
Definition: solver_manager.h:84
Definition: vector_composite.h:27
Definition: capture_base.h:23
std::string StateKeys
Definition: composite.h:31
std::size_t SizeStd
Other containers size type for the Wolf project.
Definition: wolf.h:66
Composite< std::string > TypeComposite
Definition: type_composite.h:26
Notification
Definition: problem.h:55
@ ADD
Definition: problem.h:56
@ REMOVE
Definition: problem.h:57
EIGEN_DEFAULT_DENSE_INDEX_TYPE SizeEigen
Vector and Matrices size type for the Wolf project.
Definition: wolf.h:58
std::shared_ptr< Loader > LoaderPtr
Definition: loader.h:55
Composite< Prior > PriorComposite
Definition: prior_composite.h:56